package ldh.im.fxbase.data.pojo;

import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.cache.SoftId;
import ldh.im.fxbase.data.pojo.status.*;
import ldh.rpc.util.JsonUtil;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public class ImMessage implements SoftId, Comparable<ImMessage> {

    private Long id;
    private String clientId;
    private String fromUserId;
    private String toUserId;
    private String currentUserId;
    private String serverId;
    private MessageType messageType;
    private String groupCode;
    private String content;
    private Long sessionId;                          // 会话ID
    private ImMessageRefType refType;
    private String refMessageIds;
    private String ext;
    private String ext1;
    private String ext2;
    private Long version;
    private ImMessageStatus status;
    private ImMessageFileStatus fileStatus;          // 附件的状态
    private ImMessageActionStatus actionStatus;
    private Date serverReceiveTime;
    private Date createDateTime;
    private Date updateDateTime;

    private transient MessageExt messageExt;
    private transient ImUser fromImUser;
    private transient ImUser toImUser;
    private transient ImSession imSession;
    private transient Set<ImMessageSend> messageSendSet;
    private transient List<ImMessage> refMessages;

    public ImMessage() {
        createDateTime = new Date();
        updateDateTime = createDateTime;
        version = 0L;
    }

    @Override
    public String getSoftId() {
        return getId() != null ? getId().toString() : "";
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    public ImSession getImSession() {
        return imSession;
    }

    public void setImSession(ImSession imSession) {
        this.imSession = imSession;
        this.sessionId = imSession.getId();
    }

    public ImUser getFromImUser() {
        return fromImUser;
    }

    public void setFromImUser(ImUser fromImUser) {
        this.fromImUser = fromImUser;
        if (fromImUser != null) {
            setFromUserId(fromImUser.getUserId());
        }
    }

    public void setCreateTime(Date createTime) {
        setCreateDateTime(createTime);
    }

    public Long getSessionId() {
        return sessionId;
    }

    public void setSessionId(Long sessionId) {
        this.sessionId = sessionId;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
        if (ext != null) {
            MessageExt messageExt = JsonUtil.toObject(ext, MessageExt.class);
            setMessageExt(messageExt);
        }
    }

    public MessageExt getMessageExt() {
        if (messageExt == null) {
            messageExt = new MessageExt();
        }
        return messageExt;
    }

    public void setMessageExt(MessageExt messageExt) {
        this.messageExt = messageExt;
    }

    public MessageExt initMessageExt() {
        if (ext != null && !ext.trim().equals("")) {
            messageExt = JsonUtil.toObject(ext, MessageExt.class);
        }
        return messageExt;
    }

    public void initExt() {
        if(messageExt != null) {
            ext = JsonUtil.toJson(messageExt);
        }
    }

    public String getCurrentUserId() {
        return currentUserId;
    }

    public void setCurrentUserId(String currentUserId) {
        this.currentUserId = currentUserId;
    }

    public MessageType getMessageType() {
        return messageType;
    }

    public void setMessageType(MessageType messageType) {
        this.messageType = messageType;
    }

    public String getServerId() {
        return serverId;
    }

    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    public ImMessageStatus getStatus() {
        return status;
    }

    public void setStatus(ImMessageStatus status) {
        this.status = status;
    }

    public ImMessageActionStatus getActionStatus() {
        return actionStatus;
    }

    public void setActionStatus(ImMessageActionStatus actionStatus) {
        this.actionStatus = actionStatus;
    }

    public ImUser getToImUser() {
        return toImUser;
    }

    public void setToImUser(ImUser toImUser) {
        this.toImUser = toImUser;
        if(toImUser != null) {
            setToUserId(toImUser.getUserId());
        }
    }

    public String getToUserId() {
        if (toImUser != null) return toImUser.getUserId();
        return toUserId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
//        this.setLocalSeq("pc_" + id);
    }

    public ImMessageFileStatus getFileStatus() {
        return fileStatus;
    }

    public void setFileStatus(ImMessageFileStatus fileStatus) {
        this.fileStatus = fileStatus;
    }

    public boolean isP2p() {
        return getToUserId() != null;
    }

    public boolean isSender(String userId) {
        return getFromUserId().equals(userId);
    }

    public boolean isReceiver(String userId) {
        return !isSender(userId);
    }

    public boolean isAttachFile() {
        return getMessageType() == MessageType.Image || getMessageType() == MessageType.File ||
                getMessageType() == MessageType.Audio || getMessageType() == MessageType.Video;
    }

    public Date getServerReceiveTime() {
        return serverReceiveTime;
    }

    public void setServerReceiveTime(Date serverReceiveTime) {
        this.serverReceiveTime = serverReceiveTime;
    }

    public String getFromUserId() {
        return fromUserId;
    }

    public void setFromUserId(String fromUserId) {
        this.fromUserId = fromUserId;
    }

    public void setToUserId(String toUserId) {
        this.toUserId = toUserId;
    }

    public String getGroupCode() {
        return groupCode;
    }

    public void setGroupCode(String groupCode) {
        this.groupCode = groupCode;
    }

    public Date getCreateDateTime() {
        return createDateTime;
    }

    public void setCreateDateTime(Date createDateTime) {
        this.createDateTime = createDateTime;
    }

    public Date getUpdateDateTime() {
        return updateDateTime;
    }

    public void setUpdateDateTime(Date updateDateTime) {
        this.updateDateTime = updateDateTime;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public ImMessageRefType getRefType() {
        return refType;
    }

    public void setRefType(ImMessageRefType refType) {
        this.refType = refType;
    }

    public String getRefMessageIds() {
        return refMessageIds;
    }

    public void setRefMessageIds(String refMessageIds) {
        this.refMessageIds = refMessageIds;
    }

    public String getExt1() {
        return ext1;
    }

    public void setExt1(String ext1) {
        this.ext1 = ext1;
    }

    public String getExt2() {
        return ext2;
    }

    public void setExt2(String ext2) {
        this.ext2 = ext2;
    }

    public Set<ImMessageSend> getMessageSendSet() {
        return messageSendSet;
    }

    public void setMessageSendSet(Set<ImMessageSend> messageSendSet) {
        this.messageSendSet = messageSendSet;
    }

    public boolean isGroup() {
        return groupCode != null;
    }

    public boolean isRef() {
        return refMessageIds != null;
    }

    public List<ImMessage> getRefMessages() {
        return refMessages;
    }

    public void setRefMessages(List<ImMessage> refMessages) {
        this.refMessages = refMessages;
    }

    @Override
    public int compareTo(ImMessage o) {
        return this.getId().compareTo(o.getId());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (!(obj instanceof ImMessage)) return false;
        ImMessage imMessage = (ImMessage) obj;
        if (imMessage.getId() != null && this.getId() != null) return imMessage.getId().equals(this.getId());
        if (imMessage.getServerId() != null && this.getServerId() != null) return imMessage.getServerId().equals(this.getServerId());
        return false;
    }

    @Override
    public int hashCode() {
        if (this.getId() != null) return this.getId().hashCode();
        if (this.getServerId() != null) {
            return Objects.hash(id, serverId);
        }
        return this.getContent().hashCode();
    }
}
